package com.example.demo.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-09-11
 * Time: 11:07
 */
@Aspect
@Component
public class UserAspect {

    // 定义切点，使用 AspectJ 语法
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointCut() {
    }

    // 前置通知
    @Before("pointCut()")
    public void doBefore() {
        System.out.println("执行前置通知!");
    }

    // 后置通知
    @After("pointCut()")
    public void doAfter() {
        System.out.println("执行后置通知!");
    }

    // 返回通知
    @AfterReturning("pointCut()")
    public void doAfterReturn() {
        System.out.println("执行了返回通知");
    }

    // 异常通知
    @AfterThrowing("pointCut()")
    public void doAfterThrowing() {
        System.out.println("执行了异常通知");
    }

    // 环绕通知
    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Around ⽅法开始执⾏");
        Object obj = joinPoint.proceed();
        System.out.println("Around ⽅法结束执⾏");
        return obj;
    }
}
